import { HttpMethod, HttpRequestService } from '../http';
import { MenuItem } from '../models';

/**
 * This service is intend to load the config info from json file in public/assets/config directory.
 * https://create-react-app.dev/docs/using-the-public-folder
 */
export class MenuService {
  private static readonly basePath: string = `${import.meta.env.BASE_URL}assets/config`;
  private static menuItems: MenuItem[];

  static async getLocalMenuItemConfig(): Promise<MenuItem[]> {
    if (!this.menuItems) {
      this.menuItems = (await this.loadLocalMenuConfig()) ?? new MenuItem();
    }

    return this.menuItems;
  }

  /**
   * Loads local menu config data.
   */
  private static async loadLocalMenuConfig(): Promise<MenuItem[]> {
    return (
      await new HttpRequestService().makeOriginalRequest(`${this.basePath}/menu-items.json`, {
        httpMethod: HttpMethod.get,
      })
    ).payload;
  }
}
